package com.ruoyi.bizsys.service.impl;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.bizsys.domain.ProductInfo;
import com.ruoyi.bizsys.vo.LoanOrderVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bizsys.mapper.LoanOrderMapper;
import com.ruoyi.bizsys.domain.LoanOrder;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 借款申请Service业务层处理
 *
 * @author african-loan
 * @date 2021-12-30
 */
@Service
public class LoanOrderServiceImpl extends ServiceImpl<LoanOrderMapper, LoanOrder> implements ILoanOrderService {

    @Autowired
    private LoanOrderMapper loanOrderMapper;

    /**
     * 查询借款申请
     *
     * @param id 借款申请ID
     * @return 借款申请
     */
    @Override
    public LoanOrder selectLoanOrderById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询借款申请列表
     * app端使用
     * @param loanOrder 借款申请
     * @return 借款申请
     */
    @Override
    public List<LoanOrder> selectLoanOrderList(LoanOrder loanOrder) {
        if(null !=loanOrder.getLoanStatus() &&  loanOrder.getLoanStatus() == LoanOrder.LOAN_STATUS_6_RE_INITIATE_PAY){
            return this.list(new QueryWrapper<>(loanOrder).last("order by create_time"));
        }
        return this.list(new QueryWrapper<>(loanOrder).last("order by id desc"));
    }

    /**
     * 后台使用
     * @param loanOrder
     * @return
     */
    @Override
    public List<LoanOrder> getList(LoanOrder loanOrder) {
        if(loanOrder.getParams().entrySet().size() > 0){
            return this.list(new QueryWrapper<>(loanOrder).ge("create_time",loanOrder.getParams().get("beginTime")).lt("create_time",loanOrder.getParams().get("endTime")).last("order by id desc"));
        }
        return this.list(new QueryWrapper<>(loanOrder).last("order by id desc"));
    }

    /**
     * 新增借款申请
     *
     * @param loanOrder 借款申请
     * @return 结果
     */
    @Override
    public int insertLoanOrder(LoanOrder loanOrder) {
        loanOrder.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(loanOrder);
    }

    /**
     * 修改借款申请
     *
     * @param loanOrder 借款申请
     * @return 结果
     */
    @Override
    public int updateLoanOrder(LoanOrder loanOrder) {
        loanOrder.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(loanOrder);
    }

    /**
     * 批量删除借款申请
     *
     * @param ids 需要删除的借款申请ID
     * @return 结果
     */
    @Override
    public int deleteLoanOrderByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除借款申请信息
     *
     * @param id 借款申请ID
     * @return 结果
     */
    @Override
    public int deleteLoanOrderById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<LoanOrder> getReturnRunLoanData(Integer andonTaskMinute) {
        return baseMapper.getReturnRunLoanData(andonTaskMinute);
    }

    @Override
    public List<LoanOrderVo> selectLoanOrder() {
        return loanOrderMapper.selectLoanOrder();
    }

    @Override
    public List<LoanOrderVo> selectLoanOrderPending() {
        return loanOrderMapper.selectLoanOrderPending();
    }

    @Override
    public int updateBatch(List<LoanOrder> loanOrders) {
        return loanOrderMapper.updateBatch(loanOrders);
    }

    @Override
    public LoanOrder getOneByCustNo(String custNo) {
        return this.getOne(new QueryWrapper<LoanOrder>().eq("cust_no", custNo).last("order by id desc limit 1"));
    }

    @Override
    public int countByLoanSerialNo(String loanSerialNo) {
        return this.count(new QueryWrapper<LoanOrder>().eq("loan_serial_no", loanSerialNo));
    }

    @Override
    public int todayCountByStatus(String custNo, int... loanStatus) {
        QueryWrapper<LoanOrder> queryWrapper = new QueryWrapper<LoanOrder>();
        queryWrapper.eq("cust_no", custNo).in("loan_status", loanStatus);
        queryWrapper.last("AND TO_DAYS(create_time) = TO_DAYS(NOW())");
        return count(queryWrapper);
    }

    @Override
    public LoanOrder getOneBySerialNo(String applySerialNo) {
        QueryWrapper<LoanOrder> queryWrapper = new QueryWrapper<LoanOrder>();
        queryWrapper.eq("loan_serial_no",applySerialNo);
        return getOne(queryWrapper);
    }

    @Override
    public LoanOrder pessimisticLockById(long id) {
        return baseMapper.pessimisticLockById(id);
    }

    @Override
    public LoanOrder getOneByPayTransSerialNo(String payTransSerialNo) {
        QueryWrapper<LoanOrder> queryWrapper = new QueryWrapper<LoanOrder>();
        queryWrapper.eq("pay_trans_serial_no",payTransSerialNo);
        return getOne(queryWrapper);
    }

    @Override
    public ProductInfo selectLoanOrderProduct(String custNo) {
        return loanOrderMapper.selectLoanOrderProduct(custNo);
    }

    @Override
    public int isBiggerByCustNo(String custNo, String loanSerialNo) {
        return baseMapper.isBiggerByCustNo(custNo, loanSerialNo);
    }

}
